import InputType from '@/enum/InputType'
import IButtonOptions from "@/components/ButtonGroup/button-scheme";
import {IFormItem,IFormConfig,ISelectOption} from '@/components/Form/form-scheme'
import {validatePhone} from '@/utils/validateUtils';

/**
 * 查询表单配置
 */
const searchItems: IFormItem[] =  [
    {
        model: 'username',
        type: InputType.INPUT,
        placeholder: '请输入用户名',
        label: '用户名:',
        clearable: true
    }
]
const SearchConfig: IFormConfig = {
    items: searchItems,
    labelWidth: "80px",
    span: 6
}
export {SearchConfig}

/**
 * 按钮组配置
 */
const ButtonConfig: IButtonOptions = {
    reset: {
        clickCall: 'reset',
        btnTip: '重置'
    },
    query: {
        clickCall: 'query',
        btnTip: '查询'
    },
    add: {
        clickCall: 'openDialog',
        btnTip: '添加',
        permission: ['system:user:add']
    },
    edit: {
        clickCall: 'openDialog',
        btnTip: '编辑',
        permission: ['system:user:update']
    },
    delete: {
        clickCall: 'delete',
        btnTip: '删除',
        permission: ['system:user:delete']
    }
}
export {ButtonConfig}


/**
 * 添加或更新表单配置
 */
let commonSex:ISelectOption[] = []
const formItems: IFormItem[] = [
    {
        model: 'username',
        type: InputType.INPUT,
        label: '用户名',
        placeholder: '用户名不能为admin',
        ruleName: 'username'
    },
    {
        model: 'nameZh',
        type: InputType.INPUT,
        label: '用户姓名',
        ruleName: 'nameZh'
    },
    {
        model: 'phone',
        type: InputType.INPUT,
        label: '联系电话',
        ruleName: 'phone'
    },
    {
        model: 'sex',
        type: InputType.SELECT,
        label: '性别',
        options: commonSex
    },
    {
        model: 'remark',
        type: InputType.INPUT,
        label: '备注'
    },
]



const FormConfig: IFormConfig = {
    items: formItems,
    labelWidth: '100px',
    span: 24,
    rules: {
        username: [
            {required: true, message: '请输入用户名',trigger: 'blur'},
            {min: 2, max: 10, message: '长度在2 到 10 个字符', trigger: 'blur'}
        ],
        nameZh: [
            {required: true, message: '请输入用户姓名',trigger: 'blur'},
            {min: 2, max: 10, message: '长度在2 到 10 个字符', trigger: 'blur'}
        ],
        phone: [
            {validator: validatePhone,trigger: 'blur'}
        ]
    }
}

let FormData = {
    userId: null,
    username: '',
    nameZh: '',
    phone: '',
    sex: null,
    remark: ''
}

const init = (dicts) => {
    let sexs = dicts['common_sex']
    if (sexs && sexs instanceof Array) {
        let item
        sexs.forEach(item => {
            item = {
                title: item.label,
                value: item.value
            }
            if (commonSex.indexOf(item) === -1) {
                commonSex.push(item)
            }
        })
    }
}
export {FormConfig,FormData,init}


